package self.hoy.demo.grainmall.product.app;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import self.hoy.demo.grainmall.product.entity.Brand;
import self.hoy.demo.grainmall.product.entity.CategoryBrandRelation;
import self.hoy.demo.grainmall.product.service.CategoryBrandRelationService;
import self.hoy.demo.common.utils.R;
import self.hoy.demo.grainmall.product.vo.BrandVo;


/**
 * 品牌分类关联
 *
 * @author Hoy
 * @email xxx@gmail.com
 * @date 2020-07-04 19:07:56
 */
@RestController
@RequestMapping("product/categorybrandrelation")
public class CategoryBrandRelationController {
    @Autowired
    private CategoryBrandRelationService categoryBrandRelationService;

    /**
     * 列表
     */
    @GetMapping("/catelog/list")
    //@RequiresPermissions("product:categorybrandrelation:list")
    public R list(@RequestParam("brandId") Long brandId){
        List<CategoryBrandRelation> data = categoryBrandRelationService.list(
                new QueryWrapper<CategoryBrandRelation>().eq("brand_id", brandId)
        );
        return R.ok().put("data", data);
    }

    // /product/categorybrandrelation/brands/list?t=1595006678324&catId=225
    @GetMapping("/brands/list")
    public R relationBrandList(@RequestParam(value = "catId", required = true) Long catId) {
        List<Brand> brands = categoryBrandRelationService.getBrandsByCatId(catId);
        List<BrandVo> voList = brands.stream().map( b -> {
            BrandVo v = new BrandVo();
            BeanUtils.copyProperties(b, v);
            return v;
        }).collect(Collectors.toList());
        return R.ok().put("data", voList);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("product:categorybrandrelation:info")
    public R info(@PathVariable("id") Long id){
		CategoryBrandRelation categoryBrandRelation = categoryBrandRelationService.getById(id);

        return R.ok().put("categoryBrandRelation", categoryBrandRelation);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:categorybrandrelation:save")
    public R save(@RequestBody CategoryBrandRelation categoryBrandRelation){
		categoryBrandRelationService.saveDetail(categoryBrandRelation);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:categorybrandrelation:update")
    public R update(@RequestBody CategoryBrandRelation categoryBrandRelation){
		categoryBrandRelationService.updateById(categoryBrandRelation);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:categorybrandrelation:delete")
    public R delete(@RequestBody Long[] ids){
		categoryBrandRelationService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
